


#include "dexgel\dexgel.hpp"

#include "vox_loader.h"


#include <vector>
#include <fstream>
#include <iostream>
#include <glm/glm.hpp>
//#include <glm/gtc/matrix_projection.hpp>
#include <glm/gtc/matrix_transform.hpp>

#define PACKRGB(r,g,b) \
	((unsigned int)((((0xff)&0xff)<<24)|(((r)&0xff)<<16)|(((g)&0xff)<<8)|((b)&0xff)))


#pragma comment(lib, "dexgel.lib")
int main()
{
	float xres = 600;
	float yres = 600;

	dexgel::Window App( (int)xres, (int)yres, "TorApp");

	dexgel::Rendrer *rend = App.getRendrer();

	//Mesh *voxmesh = loadVoxMesh( "duke.vox" );
	//if( voxmesh == NULL )
	//{
	//	return EXIT_FAILURE;
	//}

	//Mesh *cowmesh = loadVoxMesh( "cow.vox" );
	//if( cowmesh == NULL )
	//{
	//	return EXIT_FAILURE;
	//}

	dexgel::Mesh *kv6mesh = loadKv6AsMesh(rend, "hurricane.kv6");
	assert(kv6mesh != NULL);

	dexgel::Mesh *kv6mesh2 = loadKv6AsMesh(rend, "ball.kv6");
	assert(kv6mesh != NULL);

	glm::mat4 mCam = glm::translate( glm::mat4(1.0f), glm::vec3(0.f, 0.f, 25.f) );
	float px = 0.0f;
	float py = 0.0f;
	float pz = 25.0f;

	float hang = 0.0f;
	float vang = 0.0f;

	float oldmousx = 0.0f;
	float oldmousy = 0.0f;

	while( App.IsOpened() )
	{
		App.UpdateMessages();

		if( App.canRender() )
		{
			rend->begin(); // Begin the scene
			rend->clearColor( PACKRGB(32,32,32), 1.0f );
			rend->enableLighting();



			//rend->setLightPos( 0, glm::vec3(px, py, pz) );
			float r = 50.0f;
			float a = 6.28f * float( App.getMouseX() ) /  xres ;
			glm::vec3 light_pos = glm::vec3(-30.0f, sin(a)*r, cos(a)*r ) ;
			{
				int i=0;
				rend->setLightPos(i, light_pos);
				rend->setLightColor(i, 0xffffff, 0xffffff, 0x7f7f7f);
				rend->setLightType(i, dexgel::LIGHT_POINT);
				rend->setLightAttenuation(i, 0.01f, 0.05f, 0.0f );
				rend->setLightRange(i, 100.0f ); //50.0f * float( App.getMouseX() ) /  xres );
				rend->setLightActive(i, true);
			}



			float mouse_speed_x = App.getMouseX() - oldmousx;
			float mouse_speed_y = App.getMouseY() - oldmousy;

			if ( App.mouseDown() )
			{	
				hang -= mouse_speed_x * 1.0f;
				vang -= mouse_speed_y * 1.0f;
			}

			oldmousx = (float)App.getMouseX();
			oldmousy = (float)App.getMouseY();


			glm::vec4 dirvec = glm::vec4( App.keyDown('A')-App.keyDown('D'), 0.0f,  App.keyDown('S') - App.keyDown('W'), 1.0f );
			glm::mat4 rmx = glm::rotate( glm::mat4(1.0), vang, glm::vec3(1.f, 0.f, 0.f)  );
			glm::mat4 rmy = glm::rotate( glm::mat4(1.0), hang, glm::vec3(0.f, 1.f, 0.f)  );
			glm::mat4 rotmat = rmx * rmy;

			glm::vec3 pos = glm::vec3( px,py,pz);
			mCam = rotmat;
			glm::vec4 newdir = dirvec*rotmat;
			glm::vec3 newpos = pos + glm::vec3( newdir.x, newdir.y, newdir.z );
			px = newpos.x;
			py = newpos.y;
			pz = newpos.z;
			mCam = glm::translate( mCam, newpos );
			rend->setViewMatrix( (float*)&mCam[0] );

			rend->setProjMatrix( 60.0f * 3.14f / 180.f, xres/yres, 1.0f, 8192.0f );
			glm::mat4 identity = glm::mat4(1.0);	

			glm::mat4 mesh_1_frame(identity);// = glm::rotate( identity, 90.0f, glm::vec3(1.f, 0.f, 0.f)  );
			mesh_1_frame = glm::rotate( identity, (float)App.getTime()*20.0f, glm::vec3(0.f, 1.f, 0.f)  );
			rend->SetTransform( (float*) (&mesh_1_frame[0]) );
			rend->render(kv6mesh);

			// Show light pos
			glm::mat4 mWorld = identity;

			mWorld = glm::translate( identity, light_pos );

			rend->SetTransform( (float*) (&mWorld[0]) );
			//rend->disableLighting();
			rend->render(kv6mesh2);


			//glm::mat4 mWorld = identity;

			//for (int i=0; i<4; i++)
			//{
			//	for (int j=0; j<4; j++)
			//	{
			//		mWorld = glm::translate( rotX, glm::vec3( -19.f+i * 30.0f, -19.f + j * 30.0f, 0.0f) );
			//		
			//		rend->SetTransform( (float*) (&mWorld[0]) );
			//		//cowmesh->render();
			//		kv6mesh->render();
			//	}
			//}



			rend->end(); // Begin the scene
		}

	}

	return EXIT_SUCCESS;
}
